const fontSize = 20;

function Button(name)	// extends Sprite
{
    Sprite.apply(this);
    this.buttonMode = true;
    this.mouseChildren = false;
    this.name = name;
    //this.width = fontSize*name.length;
    this.height = fontSize + fontSize/2;
        
        
    this.t = new TextField();
    this.t.setTextFormat(Button.tFormat);
    this.t.text = name; 
    this.t.x = fontSize; 
    this.t.y = this.height/10;
    this.t.width = this.t.textWidth; 
    this.t.height = this.t.textHeight;
    
    this.setText = function(newName){
        var textChild = this.t;  
        textChild.text = newName;
        textChild.width = this.t.textWidth; 
        textChild.height = this.t.textHeight;
        this.width = this.t.width + 2*fontSize;
    }
	//
    this.width = this.t.width + 2*fontSize;
        
    this.graphics.beginFill(yellow);
    this.graphics.drawRoundRect(0, 0, this.width, this.height,10,10);
	
	
    this.bg = new Sprite();		// bg is a layer with dark blue rectangle
    this.bg.graphics.beginFill(red);
    this.bg.graphics.drawRoundRect(0, 0, this.width, this.height,10,10);
    this.addChild(this.bg);
	
    this.addChild(this.t);
        
    this.addEventListener(MouseEvent.MOUSE_OVER, this.onMOv);
    this.addEventListener(MouseEvent.MOUSE_OUT , this.onMOu);
        
    this.isIn = isIn;
    function isIn(x,y){
        var result = x > this.x;
        result &= y > this.y;
        result &= x < this.x + this.width;
        result &= y < this.y + this.height;
        return result;
    }
    
    this.setWidth = function (nWid){
        var oldW = this.width;
        this.width = nWid;
        this.x -= (nWid-oldW)/2;
        this.t.x = (this.width-this.t.textWidth)/2;
        this.bg.graphics.beginFill(red);
        this.bg.graphics.drawRoundRect(0, 0, this.width, this.height,10,10);
        this.graphics.beginFill(yellow);
        this.graphics.drawRoundRect(0, 0, this.width, this.height,10,10);
    }
}
Button.prototype = new Sprite();

//	static variable
Button.tFormat    = new TextFormat(font, fontSize, 0xffffff);

//	methods
Button.prototype.onMOv = function(e) {
    e.target.bg.visible = false;
}
Button.prototype.onMOu = function(e) {
    e.target.bg.visible = true;
}

